package com.huadi.cmcc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huadi.cmcc.domain.Order;
import com.huadi.cmcc.domain.Result;
import com.huadi.cmcc.domain.User;
import com.huadi.cmcc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author cl
 * @date 2021/8/24
 */
@RestController
    /*对订单操作*/
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    /*
    @RequestMapping(method = RequestMethod.GET)的缩写
    添加新订单
    */
    @PostMapping("/create")
    public Result create(Order order) {
        orderService.save(order);
        return Result.success();
    }
    /*
    * 管理员修改订单
    */
    @PostMapping("/update")
    public Result update(Order order) {
        orderService.updateById(order);
        return Result.success();
    }

    /**
     * 管理员查订单
     * @param page
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Order>> page(Page<Order> page, String packageName) {
        final Page<Order> resultPage = orderService.listByPage(page, packageName,null);
        return Result.success(resultPage);
    }

    /**
     * 用户查订单
     * @param page
     * @return
     */
    @GetMapping("/pageForUser")
    public Result<Page<Order>> pageForUser(Page<Order> page, String packageName, HttpSession session) {
        final User user = (User) session.getAttribute("user");
        if (user == null) {
            System.out.println("用户未登录");
            return Result.fail("用户未登录");
        }
        final Page<Order> resultPage = orderService.listByPage(page, packageName, user.getId());
        return Result.success(resultPage);
    }
    /*
    * 管理员删除订单by cl*/
    @DeleteMapping("/delete/{id}")
    /*@PathVariable接受占位符的值{id}*/
    public Result delete(@PathVariable Long id) {
        orderService.removeById(id);
        return Result.success();
    }
}
